﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using System;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace FangChanCrm.Services.Realization
{
    public class t_BeiWangLuService : It_BeiWangLuService
    {
        public readonly MyContext _Context;

        public t_BeiWangLuService(MyContext context)
        {
            _Context = context;
        }

        public t_BeiWangLu Add(t_BeiWangLu t_BeiWangLu)
        {
            _Context.t_BeiWangLu.Add(t_BeiWangLu);
            _Context.SaveChanges();
            return _Context.t_BeiWangLu.FirstOrDefault(x => x.NeiRong == t_BeiWangLu.NeiRong && x.RenYuanId == t_BeiWangLu.RenYuanId && x.TiXingShiJian == t_BeiWangLu.TiXingShiJian && x.BiaoTi == t_BeiWangLu.BiaoTi);
        }

        public bool Delete(int id)
        {
            _Context.t_RenYuanBiao.Remove(_Context.t_RenYuanBiao.FirstOrDefault(x => x.Id == id));
            return _Context.SaveChanges() > 0;
        }

        public t_BeiWangLu GetById(int id)
        {
            return _Context.t_BeiWangLu.Include(x => x.t_RenYuanBiao).FirstOrDefault(x => x.Id == id);
        }

        public List<t_BeiWangLu> GetList(int id)
        {
            return _Context.t_BeiWangLu.Include(x => x.t_RenYuanBiao).Where(x => x.RenYuanId == id).ToList();
        }

        public string GetListWithFenYe(t_BeiWangLu t_BeiWangLu, int start, int limit, string dir, int column)
        {
            var list = _Context.t_BeiWangLu.ToList();
            switch (column)
            {
                case 0:
                    if (dir == "asc")
                    {
                        list = list.OrderBy(x => x.Id).ToList();
                    }
                    else
                    {
                        list = list.OrderByDescending(x => x.Id).ToList();
                    }
                    break;
                default:
                    break;
            }
            return Helper.ListToJsonAddFenYe(list, start, limit);
        }

        public string ReturnWhere(t_BeiWangLu t_BeiWangLu)
        {
            string ReturnStr = "1=1";
            if (t_BeiWangLu.Id > 0)
            {
                ReturnStr = ReturnStr + " and Id == " + t_BeiWangLu.Id;
            }
            if (t_BeiWangLu.BiaoTi != null)
            {
                ReturnStr = ReturnStr + " and BiaoTi.Contains(\"" + t_BeiWangLu.BiaoTi + "\")";
            }
            if (t_BeiWangLu.NeiRong != null)
            {
                ReturnStr = ReturnStr + " and NeiRong.Contains(\"" + t_BeiWangLu.NeiRong + "\")";
            }
            if (t_BeiWangLu.RenYuanId > 0)
            {
                ReturnStr = ReturnStr + " and RenYuanId == " + t_BeiWangLu.RenYuanId;
            }
            if (t_BeiWangLu.TiXingShiJian != null)
            {
                ReturnStr = ReturnStr + " and TiXingShiJian.Contains(\"" + t_BeiWangLu.TiXingShiJian + "\")";
            }
            return ReturnStr;
        }

        
    }
}
